package com.yuyou.fn.crm.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.Principals;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.crm.entity.CustomerResource;
import com.yuyou.fn.crm.entity.Visit;
import com.yuyou.fn.crm.mapper.VisitMapper;
import com.yuyou.fn.crm.service.ICustomerResourceService;
import com.yuyou.fn.crm.service.IVisitService;
import com.yuyou.fn.crm.vo.VisitSaveVo;
import com.yuyou.fn.educational.service.IBaseGradeService;
import com.yuyou.fn.educational.service.IBaseSubjectService;
import org.apache.commons.lang3.Validate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 * Visit 表数据服务层接口实现类
 *
 */
@Service
public class VisitServiceImpl extends SuperServiceImpl<VisitMapper, Visit,Long> implements IVisitService {

    @Resource
    private ICustomerResourceService customerResourceService;

    @Resource
    private IBaseGradeService baseGradeService;

    @Resource
    private IBaseSubjectService baseSubjectService;

    @Override
    @Transactional
    public void saveVisit(VisitSaveVo saveVo, Principals.Principal principal) {
        Validate.notNull(saveVo.getCustomerResourceId(),"Required ");
        Validate.notNull(saveVo.getWay(),"Required way");
        Validate.notNull(saveVo.getRemark(),"Required remark");
        Validate.notNull(saveVo.getVisitTime(),"Required visitTime");
        Validate.notNull(saveVo.getRegPossible(),"Required regPossible");

        CustomerResource customerResource = customerResourceService.findById(saveVo.getCustomerResourceId());

        if(!customerResource.getFollowOperatorId().equals(principal.id)){
            throw new BusException("你不是抢占人");
        }
        if (saveVo.getAgainVisitStatus()==null){
            throw new BusException("需选择是否再次跟进");
        }
        if ( saveVo.getAgainVisitStatus()==1 && saveVo.getAgainVisitTime()==null){
            throw new BusException("设置继续跟进时时间必选");
        }else if (saveVo.getAgainVisitStatus()==1 && saveVo.getAgainVisitTime()!=null){
            if (saveVo.getAgainVisitTime().compareTo(TimeUtils.changeOneDateLastTime(TimeUtils.nowDate()))!=1){
                throw new BusException("再次跟进日期必须大于当前日期");
            }
        }

        Visit visit = new Visit();
        BeanUtils.copyProperties(visit,saveVo);
        visit.setStudentId(customerResource.getStudentId());
        visit.setVisitOperatorId(principal.id);
        visit.setVisitOperatorName(principal.name);
        visit.setUpdateTime(TimeUtils.now());
        visit.setCreateTime(TimeUtils.now());
        if(visit.getGradeId() != null ){
            visit.setGradeName( baseGradeService.findGradeNameById(visit.getGradeId()) );
        }

        if(visit.getSubjectId() != null ){
            visit.setSubjectName(baseSubjectService.findGradNameById(visit.getSubjectId()));
        }

        CustomerResource customerResourceUpdate = new CustomerResource();
        customerResourceUpdate.setCustomerResourceId(customerResource.getCustomerResourceId());
        customerResourceUpdate.setAgainVisitStatus(saveVo.getAgainVisitStatus());
        customerResourceUpdate.setAgainVisitTime(saveVo.getAgainVisitTime());
        customerResourceUpdate.setVisitStatus(1);
        customerResourceUpdate.setLastVisitTime(TimeUtils.now());
        customerResourceUpdate.setVisitTimes(customerResource.getVisitTimes() == null ? 1 : customerResource.getVisitTimes() + 1 );
        customerResourceService.updateSelectiveById(customerResourceUpdate);

        insertSelective(visit);
    }

    @Override
    public List<Visit> findVisitListByCustomerResourceId(Long customerResourceId) {
        Validate.notNull(customerResourceId,"Required ");
        Example<Visit> example=Visit.createExample();
        example.createCriteria().andEqualTo("customerResourceId",customerResourceId);
        return this.findListByExample(example);
    }

    @Override
    public void deleteByCustomerResourceIds(List<Long> customerResourceIds) {
        if (!customerResourceIds.isEmpty()){
            Example<Visit> example=Visit.createExample();
            example.createCriteria().andIn("customerResourceId",customerResourceIds);
            this.deleteByExample(example);
        }
    }
}